<?php
namespace Admin\Controller;
use Think\Controller;

use Alidayu\AlidayuClient as Client;
use Alidayu\Request\SmsNumSend;

class HomepageController extends Controller {

    /**
     * 修改首页轮播图
     **/
    public function index(){
        if (IS_POST) {
            $data = I();
            $data['updata_time'] = time();
            if (empty($data['images'])){
                $data['images'] = array();
            }else{
                foreach ($data['images'] as $k => $banenr_image)
                {
                    $data['images'][$k] = base64ToImage($banenr_image);
                }
            }
            if (empty($data['old_banenr_images'])){
                $data['old_banenr_images'] = array();
            }

            $firstdata=M('homepage')->where(array('id' => 1))->find();
            if($firstdata){
                $data['images'] = array_merge($data['old_banenr_images'], $data['images']);
                $data['images'] = implode(',', $data['images']);
                $data['id']=1;
                M('homepage')->where(array('id' => 1))->setField($data)?$this->success('修改成功'):$this->error('修改失败');
            }else{
                $data['images'] = array_merge($data['old_banenr_images'], $data['images']);
                $data['images'] = implode(',', $data['images']);
                $data['id']=1;
                M('homepage')->add($data)?$this->success('添加成功'):$this->error('添加成功');
            }
        } else{
            $data = M('homepage')->where(array('id' => 1))->find();
            $images = explode(',', $data['images']);
            foreach ($images as $k => $v) {
                if (!$v) unset($images[$k]);
            }
            $this->assign('images', $images);
            $this->assign('data', $data);
            $this->display();
        }
    }

    /**
     *修改图标
     **/
    public function icon(){
        if (IS_POST) {
            $data = I();
            $data['add_time'] = time();
            $data['images']['icon'] =$data['icon']? base64ToImage($data['icon']):$data['old_icon'];
            $data['images']['icon2'] =$data['icon2']? base64ToImage($data['icon2']):$data['old_icon2'];
            $data['images']['icon3'] =$data['icon3']? base64ToImage($data['icon3']):$data['old_icon3'];
            $data['images']['icon4'] =$data['icon4']? base64ToImage($data['icon4']):$data['old_icon4'];
            $data['images']['icon5'] =$data['icon5']? base64ToImage($data['icon5']):$data['old_icon5'];
            $data['images']['icon6'] =$data['icon6']? base64ToImage($data['icon6']):$data['old_icon6'];
            $data['images']['icon7'] =$data['icon7']? base64ToImage($data['icon7']):$data['old_icon7'];
            $data['images']['icon8'] =$data['icon8']? base64ToImage($data['icon8']):$data['old_icon8'];
            $datas['icon']=implode(',',$data['images']);
            $datas['icon_name']=implode(',',$data['icon_name']);
            $datas['icon_url']=implode(',',$data['icon_url']);
            M('homepage')->where(array('id'=>1))->setField($datas)?$this->success('修改成功',U('Homepage/icon')):$this->error('无修改内容，修改失败');
        } else{
            $data = M('homepage')->where(array('id' => 1))->find();
            $data_icon=explode(',',$data['icon']);
            $data_name=explode(',',$data['icon_name']);
            $data_url=explode(',',$data['icon_url']);
            $this->assign('dataimg',$data_icon);
            $this->assign('dataname',$data_name);
            $this->assign('dataurl',$data_url);
            $this->display();
        }
    }
}